package com.xdw.szpxapp.provider;

import com.xdw.szpxapp.ResourceTable;
import com.xdw.szpxapp.model.Contacts;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class ContactsProvider extends BaseItemProvider {
    private List<Contacts> contactsList;
    private Context context;

    public ContactsProvider(List<Contacts> contactsList, Context context) {
        this.contactsList = contactsList;
        this.context = context;
    }

    public ContactsProvider() {
    }

    @Override
    public int getCount() {
        return contactsList.size();
    }

    @Override
    public Object getItem(int position) {
        return contactsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        if(component==null){
            component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_contacts,null,false);
        }
        Image ivIcon = (Image)component.findComponentById(ResourceTable.Id_iv_icon);
        Text tvName = (Text)component.findComponentById(ResourceTable.Id_tv_name);
        tvName.setText(contactsList.get(position).getName());
        if("男".equals(contactsList.get(position).getGender())){
            ivIcon.setImageAndDecodeBounds(ResourceTable.Media_man);
        }else {
            ivIcon.setImageAndDecodeBounds(ResourceTable.Media_lady);
        }
        return component;
    }
}
